const api = require('../../api/api.js');
const app = getApp();

Page({
  data: {
    places: [],
    placeIds: '',
    currentTab: 0,
    imgUrl: app.globalData.imgUrl + 'siteImage/',
    placeImg: app.globalData.imgUrl + 'placeImage/'
  },
  onLoad(o) {
    let siteId = o.siteId;
    this.setData({
      siteId: siteId
    });
    this.querySiteImages(siteId);
    this.querySite(siteId);
    this.queryPlaces(siteId);
  },
  queryPlaces(siteId) {
    let that = this;
    api.queryPlaces({
      data: {
        placeId: -1,
        siteId: siteId,
        limit: -1,
        pageNumber: 0,
        type: ''
      },
      success(res) {
        console.log(res.data);
        let places = res.data.places;
        let placeIds = '';
        let placeIdArr = [];
        places.map((i) => {
          placeIds += i.placeId + ',';
          placeIdArr.push(i.placeId);
        });
        that.queryPlaceImage(places, 0);
        that.setData({
          placeIds: placeIds.substring(0, placeIds.length - 1)
        });
      }
    });
  },
  queryPlaceImage(places, count) {
    if (places.length === count) {
      this.setData({
        places
      });
      return;
    }
    let that = this;
    let placeId = places[count].placeId;
    api.queryPlaceImage({
      data: {
        placeId
      },
      success(res) {
        console.log(res.data);
        let pimgs = res.data.placeImages;
        if (pimgs.length > 0) {
          places[count].image = pimgs[0].imgUrl;
        } else {
          places[count].image = 'noImage.png';
        }
        that.queryPlaceImage(places, ++count);
      }
    });
  },
  querySite(siteId) {
    let that = this;
    api.querySite({
      data: {
        siteId: siteId
      },
      success(res) {
        console.log(res.data);
        that.setData({
          site: res.data.site
        });
      }
    });
  },
  querySiteImages(siteId) {
    let that = this;
    let imgUrl = this.data.imgUrl;
    api.querySiteImages({
      data: {
        siteId: siteId
      },
      success(res) {
        console.log(res.data);
        let images = res.data.siteImages;
        if (images.length === 0) {
          images = [imgUrl + 'noImage.png'];
        } else {
          images = images.map((i) => {
            return imgUrl + i.image
          });
        }
        that.setData({
          images: images
        });
      }
    });
  },
  previewImage(e) {
    let url = e.currentTarget.dataset.url;
    let urls = this.data.images;
    wx.previewImage({
      current: url,
      urls: urls
    })
  },
  switchSwiper(e) {
    let currentTab = e.detail.current;
    let left = 50 * currentTab + 17.5 + '%';
    this.barAnimal(left);
  },
  clickTab(e) {
    let currentId = e.currentTarget.dataset.index;
    this.setData({
      currentTab: currentId
    });
  },
  barAnimal(left) {
    let animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-in-out'
    });
    animation.left(left).step();
    this.setData({
      barAnimal: animation.export()
    });
  },
  goToSiteMapPage() {
    let siteId = this.data.siteId;
    let placeIds = this.data.placeIds;
    wx.navigateTo({
      url: `/pages/sitemap/sitemap?siteId=${siteId}&placeIds=${placeIds}`,
    })
  },
  goToPanoramaPage() {
    let siteId = this.data.siteId;
    wx.navigateTo({
      url: `/pages/panorama/panorama?siteId=${siteId}`,
    })
  },
  goToPlacePage(e) {
    let placeId = e.currentTarget.dataset.id;
    api.queryPlaceInfo({
      data: {
        placeId
      },
      success(res) {
        let pi = res.data.placeInfos;
        if (pi.length === 0) {
          wx.showToast({
            title: '该地块信息未完善，无法查看！',
            icon: 'none'
          });
        } else {
          wx.navigateTo({
            url: `/pages/place/place?placeId=${placeId}`,
          })
        }
      }
    });
  }
})